//
//  MSServiceTerminalController.m
//  觅尚
//
//  Created by No.8 on 2019/4/4.
//  Copyright © 2019 No.8. All rights reserved.
//

#import "MSServiceTerminalHomeController.h"
#import <CoreLocation/CoreLocation.h>
#import "MSlocationButton.h"
#import "MSlocationIMGleftButton.h"
#import "MSAttendanceCardController.h"
#import "DatePickerView.h"
#import <JZLocationConverter.h>


@interface MSServiceTerminalHomeController ()<CLLocationManagerDelegate,UITableViewDelegate,UITableViewDataSource>
{
    CLLocationManager *locationmanager;//定位服务
    NSString *currentCity;//当前城市
    CLLocation *Location;//经度
    NSString *strlongitude;//纬度
    NSInteger  secondsCountDown;
    
    
    
    
    
}

/** 具体位置*/
@property(nonatomic,strong)NSString * locationName;


/** 打卡状态*/
@property(nonatomic,strong)NSString * attendTypeStr;


//上班显示时间
@property(nonatomic,strong)UIButton * ClockButton;

//下班显示时间
@property(nonatomic,strong)UIButton * XiaClockButton;

@property(nonatomic,strong)UIButton * TimeButton;


//下班按钮
@property(nonatomic,strong)UIButton * XiaTimeButton;


@property(nonatomic,strong)MSlocationIMGleftButton * IMGleftButton;


/********** 上班线********/
/** 上班线*/
@property(nonatomic,strong)UIView *lineView;


/********** 下班线********/
/** 下班线*/
@property(nonatomic,strong)UIView *XialineView;



/** 日期*/
@property(nonatomic,strong)UIButton *DateButton;


//创建定时器(因为下面两个方法都使用,所以定时器拿出来设置为一个属性)
@property(nonatomic,strong)NSTimer*countDownTimer;

/** 上班时间*/
@property(nonatomic,strong)UILabel *JobTimeTitle;


/** 上班标签*/
@property(nonatomic,strong)UILabel *JobTitle;

/** 下班标签*/
@property(nonatomic,strong)UILabel *LowerTitle;


/** 下班标签*/
@property(nonatomic,strong)UILabel *XiaLowerTitle;


/** 下班时间*/
@property(nonatomic,strong)UILabel *LowerTimeTitle;


/** 下班时间*/
@property(nonatomic,strong)UILabel *XiaLowerTimeTitle;

/** 范围按钮*/
@property(nonatomic,strong)MSlocationButton *locationButton;

@property (nonatomic, strong) DatePickerView *datePickerView;

@property (nonatomic, assign)double jingdu;

@property (nonatomic, assign)double weidu;

/** <#name#>*/
@property(nonatomic,strong)UILabel *upWorkStrutsLabel;

/** <#name#>*/
@property(nonatomic,strong)UILabel *closeWorkStrutsLabel;


/** 表视图*/
@property(nonatomic,strong)UITableView *tableView;

/** 状态*/
@property(nonatomic,strong)NSString * status;

@end

@implementation MSServiceTerminalHomeController


/** Nav实现 */
- (int)navBarBottom {
    if ([WRNavigationBar isIphoneX]) {
        return 88;
    } else {
        return 64;
    }
}
- (void)viewWillAppear:(BOOL)animated {
    [super viewWillAppear:YES];
    self.navigationController.navigationBar.hidden = YES;
    self.view.backgroundColor = backgroundcolor;
        NSDate *date=[NSDate date];
        NSDateFormatter *format1=[[NSDateFormatter alloc] init];
        [format1 setDateFormat:@"HH:mm:ss"];
        NSString *dateStr;
        dateStr = [format1 stringFromDate:date];
        NSLog(@"%@",dateStr);
        NSString *timeSp = [NSString stringWithFormat:@"%ld", (long)[date timeIntervalSince1970]];
    
        secondsCountDown = [timeSp integerValue];
    
    Prepare *header = [Prepare headerWithRefreshingTarget:self refreshingAction:@selector(MJChiBaoZiHeaderRefresh)];
    // 隐藏时间
    header.lastUpdatedTimeLabel.hidden = YES;
    header.stateLabel.hidden = YES;
    // 马上进入刷新状态
    self.tableView.mj_header = header;
    [self.tableView.mj_header beginRefreshing];
        
    
    
    
}
- (void)viewWillDisappear:(BOOL)animated {
    [super viewWillDisappear:YES];
    self.navigationController.navigationBar.hidden = YES;
    
    [locationmanager stopUpdatingLocation];
    
}
- (void)MJChiBaoZiHeaderRefresh{
    [self ReloadData];
}

/** 表视图*/
- (void)CreattableView{
    if (@available(iOS 11.0, *)) {
        
        UIScrollView.appearance.contentInsetAdjustmentBehavior = UIScrollViewContentInsetAdjustmentNever;
        
    } else {
        
        self.automaticallyAdjustsScrollViewInsets = NO;
        
    }
    
    self.tableView = [[UITableView alloc]initWithFrame:CGRectMake(0, 0, ScreenWidth, ScreenHeight) style:UITableViewStylePlain];
    self.tableView.delegate = self;
    self.tableView.dataSource = self;
    self.tableView.backgroundColor = [UIColor clearColor];
    self.tableView.tableFooterView = [[UIView alloc] initWithFrame:CGRectZero];
    self.tableView.separatorStyle = UITableViewCellSeparatorStyleNone;
    
    //    self.tableView.scrollEnabled = NO;
    [self.view addSubview:self.tableView];
    //    }
    //    return _tableView;
}
- (void)ReloadData{
    
    NSDictionary * params = @{@"busid":[WYCAccountTool unarchivebelongBusid],@"uid":[WYCAccountTool unarchiveuid]};
    
    [HTTPRequest POST:MSgetBussinessWorkTime params:params succeed:^(id responseObject) {
        
        WYCLog(@"-=-------%@",responseObject);
        
        if (CodeStr){
            [self.tableView.mj_header endRefreshing];
            [self.DateButton setTitle:[NSString stringWithFormat:@"%@",responseObject[@"data"][@"businessData"][@"nowDate"]] forState:UIControlStateNormal];
                self.JobTimeTitle.text = [NSString stringWithFormat:@"%@",responseObject[@"data"][@"upWorkAttendance"]];
                self.LowerTimeTitle.text = [NSString stringWithFormat:@"%@",responseObject[@"data"][@"closeAttendance"]];
                self.XiaLowerTimeTitle.text = [NSString stringWithFormat:@"%@",responseObject[@"data"][@"closeAttendance"]];
            double latitude = [responseObject[@"data"][@"businessData"][@"latitude"] doubleValue];
            double longitude = [responseObject[@"data"][@"businessData"][@"longitude"] doubleValue];
            CLLocation *curLocation = [[CLLocation alloc] initWithLatitude:self.jingdu longitude:self.weidu];
            CLLocation *otherLocation = [[CLLocation alloc] initWithLatitude:latitude longitude:longitude];
            double  distance  = [curLocation distanceFromLocation:otherLocation];
            NSLog(@"距离:%f",distance);
            
            // 计算距离
            self.attendTypeStr = [NSString stringWithFormat:@"%@",responseObject[@"data"][@"status"]];
            
            

            
            double compare = [responseObject[@"data"][@"businessData"][@"attendanceRange"] doubleValue];
            if (compare + 80.000000 <= distance) {
                [self.locationButton setTitle:@"未进入考勤范围" forState:UIControlStateNormal];
                self.TimeButton.enabled = NO;
                [self.locationButton setImage:[UIImage imageNamed:@"感叹号"] forState:UIControlStateNormal];
            }else{

                [self.locationButton setTitle:@"已进入考勤范围" forState:UIControlStateNormal];
                self.TimeButton.enabled = YES;
                [self.locationButton setImage:[UIImage imageNamed:@"jinrufanwei"] forState:UIControlStateNormal];
            }
            
            
            
            
            if ([responseObject[@"data"][@"upWorkStruts"] intValue] == 7) {
                //未打卡
                
                
                //未到打卡时间
                if ([responseObject[@"data"][@"status"] intValue] == 1) {
                    [self.TimeButton setTitle:@"未到打卡时间" forState:UIControlStateNormal];
//                    self.TimeButton.enabled = NO;
                    [self.TimeButton setBackgroundImage:[UIImage imageNamed:@"圆"] forState:UIControlStateNormal];
                    self.upWorkStrutsLabel.hidden = YES;
                    self.closeWorkStrutsLabel.hidden = YES;
                    self.XialineView.hidden = YES;
                    self.XiaLowerTitle.hidden = YES;
                    self.XiaLowerTimeTitle.hidden = YES;
                    self.lineView.hidden = NO;
                    self.LowerTitle.hidden = NO;
                    self.LowerTimeTitle.hidden = NO;
                    self.TimeButton.hidden = NO;
                    self.locationButton.frame = CGRectMake(ScreenWidth / 3, CGRectGetMaxY(self.TimeButton.frame)+ 30 * px, ScreenWidth / 3, 60 * px);
                }
                
                //正常上班打卡
                else if ([responseObject[@"data"][@"status"] intValue] == 2){
                    [self.TimeButton setBackgroundImage:[UIImage imageNamed:@"daka_bg"] forState:UIControlStateNormal];
                    self.TimeButton.frame = CGRectMake(ScreenWidth / 2 - 200 * px, CGRectGetMaxY(self.IMGleftButton.frame) + 50 * px, 400 * px, 400 * px);
                    self.locationButton.frame = CGRectMake(ScreenWidth / 3, CGRectGetMaxY(self.TimeButton.frame)+ 30 * px, ScreenWidth / 3, 60 * px);
                    [self.TimeButton setTitle:@"上班打卡" forState:UIControlStateNormal];
                    self.JobTitle.backgroundColor = FontColor;
                    self.XiaLowerTitle.hidden = YES;
                    self.XiaLowerTimeTitle.hidden = YES;
                    self.LowerTimeTitle.hidden = NO;
                    self.closeWorkStrutsLabel.hidden = YES;
                    self.upWorkStrutsLabel.hidden = YES;
                    self.LowerTitle.backgroundColor = [UIColor grayColor];
                    
                    
                }
                
                //未打卡迟到打卡
                else if ([responseObject[@"data"][@"status"] intValue] == 3){
                    [self.TimeButton setBackgroundImage:[UIImage imageNamed:@"daka_bg"] forState:UIControlStateNormal];
                    [self.TimeButton setTitle:@"迟到打卡" forState:UIControlStateNormal];
                    self.TimeButton.frame = CGRectMake(ScreenWidth / 2 - 200 * px, CGRectGetMaxY(self.IMGleftButton.frame) + 50 * px, 400 * px, 400 * px);
                    self.locationButton.frame = CGRectMake(ScreenWidth / 3, CGRectGetMaxY(self.TimeButton.frame)+ 30 * px, ScreenWidth / 3, 60 * px);
                    self.JobTitle.backgroundColor = FontColor;
                    self.XiaLowerTitle.hidden = YES;
                    self.XiaLowerTimeTitle.hidden = YES;
                    self.LowerTimeTitle.hidden = NO;
                    self.closeWorkStrutsLabel.hidden = YES;
                    self.upWorkStrutsLabel.hidden = YES;
                    self.LowerTitle.backgroundColor = [UIColor grayColor];
                }
            }else{
                //已打卡
                if ([responseObject[@"data"][@"status"] intValue] == 1) {
                    [self.TimeButton setTitle:@"未到打卡时间" forState:UIControlStateNormal];
                    
                    self.TimeButton.enabled = NO;
                }else if ([responseObject[@"data"][@"status"] intValue] == 2){
                    
                    self.TimeButton.frame = CGRectMake(ScreenWidth / 2 - 200 * px, CGRectGetMaxY(self.XiaLowerTitle.frame) + 50 * px, 400 * px, 400 * px);
                    self.locationButton.frame = CGRectMake(ScreenWidth / 3, CGRectGetMaxY(self.TimeButton.frame)+ 30 * px, ScreenWidth / 3, 60 * px);
                    [self.TimeButton setBackgroundImage:[UIImage imageNamed:@"圆"] forState:UIControlStateNormal];
                    
                    [self.TimeButton setTitle:@"未到打开时间" forState:UIControlStateNormal];
                    self.JobTitle.backgroundColor = [UIColor grayColor];
                    self.upWorkStrutsLabel.hidden = NO;
                    self.XiaLowerTitle.hidden = NO;
                    self.XiaLowerTimeTitle.hidden = NO;
                    self.XiaLowerTitle.backgroundColor = FontColor;
                    self.LowerTitle.hidden = YES;
                    self.lineView.hidden = YES;
                    self.LowerTimeTitle.hidden = YES;
                    self.upWorkStrutsLabel.text = @"签到";
                    self.closeWorkStrutsLabel.hidden = YES;
                    
                    
                }else if ([responseObject[@"data"][@"status"] intValue] == 3){
                    [self.TimeButton setBackgroundImage:[UIImage imageNamed:@"圆"] forState:UIControlStateNormal];
                    self.TimeButton.frame = CGRectMake(ScreenWidth / 2 - 200 * px, CGRectGetMaxY(self.XiaLowerTitle.frame) + 50 * px, 400 * px, 400 * px);
                    self.locationButton.frame = CGRectMake(ScreenWidth / 3, CGRectGetMaxY(self.TimeButton.frame)+ 30 * px, ScreenWidth / 3, 60 * px);
                    [self.TimeButton setTitle:@"未到打卡时间" forState:UIControlStateNormal];
                    self.JobTitle.backgroundColor = [UIColor grayColor];
                    self.upWorkStrutsLabel.hidden = NO;
                    self.XiaLowerTitle.hidden = NO;
                    self.XiaLowerTimeTitle.hidden = NO;
                    self.XiaLowerTitle.backgroundColor = FontColor;
                    self.LowerTitle.hidden = YES;
                    self.lineView.hidden = YES;
                    self.LowerTimeTitle.hidden = YES;
                    self.upWorkStrutsLabel.text = @"迟到";
                    self.closeWorkStrutsLabel.hidden = YES;
//                    self.TimeButton.enabled = NO;
                }
            }
            
            
            
            
            
            //下班状态
            
            if ([responseObject[@"data"][@"closeWorkStruts"] intValue] == 7) {

                
                if ([responseObject[@"data"][@"status"] intValue] == 4){
                    [self.TimeButton setBackgroundImage:[UIImage imageNamed:@"daka_bg"] forState:UIControlStateNormal];
                    self.TimeButton.frame = CGRectMake(ScreenWidth / 2 - 200 * px, CGRectGetMaxY(self.XiaLowerTitle.frame) + 50 * px, 400 * px, 400 * px);
                    self.locationButton.frame = CGRectMake(ScreenWidth / 3, CGRectGetMaxY(self.TimeButton.frame)+ 30 * px, ScreenWidth / 3, 60 * px);
                    [self.TimeButton setTitle:@"早退打卡" forState:UIControlStateNormal];
                    self.XiaLowerTitle.backgroundColor = FontColor;
                    self.lineView.hidden = YES;
                    self.LowerTitle.hidden = YES;
                    self.LowerTimeTitle.hidden = YES;
                    self.upWorkStrutsLabel.hidden = NO;
                    self.closeWorkStrutsLabel.hidden = YES;
                    if ([responseObject[@"data"][@"upWorkStruts"] intValue] == 2) {
                        self.upWorkStrutsLabel.text = @"签到";
                    }else if ([responseObject[@"data"][@"upWorkStruts"] intValue] == 3) {
                        self.upWorkStrutsLabel.text = @"迟到";
                    }else if ([responseObject[@"data"][@"upWorkStruts"] intValue] == 7) {
                        self.upWorkStrutsLabel.text = @"缺卡";
                    }
                    
                    
                }else if ([responseObject[@"data"][@"status"] intValue] == 5){
                    [self.TimeButton setBackgroundImage:[UIImage imageNamed:@"daka_bg"] forState:UIControlStateNormal];
                    [self.TimeButton setTitle:@"下班打卡" forState:UIControlStateNormal];
                    self.TimeButton.frame = CGRectMake(ScreenWidth / 2 - 200 * px, CGRectGetMaxY(self.XiaLowerTitle.frame) + 50 * px, 400 * px, 400 * px);
                    self.locationButton.frame = CGRectMake(ScreenWidth / 3, CGRectGetMaxY(self.TimeButton.frame)+ 30 * px, ScreenWidth / 3, 60 * px);
                    self.LowerTitle.hidden = YES;
                    self.LowerTimeTitle.hidden = YES;
                    self.XiaLowerTimeTitle.hidden = NO;
//                    self.upWorkStrutsLabel.hidden = YES;
                    self.XialineView.hidden = NO;
                    self.lineView.hidden = YES;
                    self.XiaLowerTitle.backgroundColor = FontColor;
                    self.upWorkStrutsLabel.hidden = NO;
//                    if ([responseObject[@"data"][@"status"] intValue] == 4){
                        self.upWorkStrutsLabel.hidden = NO;
                        if ([responseObject[@"data"][@"upWorkStruts"] intValue] == 2) {
                            self.upWorkStrutsLabel.text = @"签到";
                        }else if ([responseObject[@"data"][@"upWorkStruts"] intValue] == 3) {
                            self.upWorkStrutsLabel.text = @"迟到";
                        }else if ([responseObject[@"data"][@"upWorkStruts"] intValue] == 7) {
                            self.upWorkStrutsLabel.text = @"缺卡";
                        }
//                    }
                    self.closeWorkStrutsLabel.hidden = YES;
                    
                }else if ([responseObject[@"data"][@"status"] intValue] == 6){
                    [self.TimeButton setTitle:@"未到打卡时间" forState:UIControlStateNormal];
                    self.TimeButton.enabled = NO;
                    [self.TimeButton setBackgroundImage:[UIImage imageNamed:@"圆"] forState:UIControlStateNormal];
                }
            }else{
                

                    if ([responseObject[@"data"][@"status"] intValue] == 4){
                        
                        self.upWorkStrutsLabel.hidden = NO;
                        if ([responseObject[@"data"][@"upWorkStruts"] intValue] == 2) {
                            self.upWorkStrutsLabel.text = @"签到";
                        }else if ([responseObject[@"data"][@"upWorkStruts"] intValue] == 3) {
                            self.upWorkStrutsLabel.text = @"迟到";
                        }else if ([responseObject[@"data"][@"upWorkStruts"] intValue] == 7) {
                            self.upWorkStrutsLabel.text = @"缺卡";
                        }
                    self.LowerTitle.hidden = YES;
                    self.XiaLowerTitle.hidden = NO;
                    [self.TimeButton setTitle:@"今日已打卡" forState:UIControlStateNormal];
                    self.TimeButton.enabled = NO;
                    self.TimeButton.frame = CGRectMake(ScreenWidth / 2 - 200 * px, CGRectGetMaxY(self.XiaLowerTitle.frame) + 50 * px, 400 * px, 400 * px);
                    self.locationButton.frame = CGRectMake(ScreenWidth / 3, CGRectGetMaxY(self.TimeButton.frame)+ 30 * px, ScreenWidth / 3, 60 * px);
                    [self.TimeButton setBackgroundImage:[UIImage imageNamed:@"圆"] forState:UIControlStateNormal];
                    self.closeWorkStrutsLabel.hidden = NO;
                        if ([responseObject[@"data"][@"closeWorkStruts"] intValue] == 4) {
                            self.closeWorkStrutsLabel.text = @"早退";
                        }else if ([responseObject[@"data"][@"closeWorkStruts"] intValue] == 5) {
                            self.closeWorkStrutsLabel.text = @"签退";
                        }else if ([responseObject[@"data"][@"closeWorkStruts"] intValue] == 7) {
                            self.closeWorkStrutsLabel.text = @"缺卡";
                        }
                        self.lineView.hidden = YES;
                        self.LowerTimeTitle.hidden = YES;
                        
                }else if ([responseObject[@"data"][@"status"] intValue] == 5){
                    [self.TimeButton setTitle:@"今日已打卡" forState:UIControlStateNormal];
                    self.TimeButton.frame = CGRectMake(ScreenWidth / 2 - 200 * px, CGRectGetMaxY(self.XiaLowerTitle.frame) + 50 * px, 400 * px, 400 * px);
                    self.locationButton.frame = CGRectMake(ScreenWidth / 3, CGRectGetMaxY(self.TimeButton.frame)+ 30 * px, ScreenWidth / 3, 60 * px);
                    [self.TimeButton setBackgroundImage:[UIImage imageNamed:@"圆"] forState:UIControlStateNormal];
                    
                    self.TimeButton.enabled = NO;
                    self.closeWorkStrutsLabel.hidden = NO;
//                    if ([responseObject[@"data"][@"status"] intValue] == 4){
                        self.upWorkStrutsLabel.hidden = NO;
                        if ([responseObject[@"data"][@"upWorkStruts"] intValue] == 2) {
                            self.upWorkStrutsLabel.text = @"签到";
                        }else if ([responseObject[@"data"][@"upWorkStruts"] intValue] == 3) {
                            self.upWorkStrutsLabel.text = @"迟到";
                        }else if ([responseObject[@"data"][@"upWorkStruts"] intValue] == 7) {
                            self.upWorkStrutsLabel.text = @"缺卡";
                        }
//                    }
                    
                    
                    
                    if ([responseObject[@"data"][@"closeWorkStruts"] intValue] == 4) {
                        self.closeWorkStrutsLabel.text = @"早退";
                    }else if ([responseObject[@"data"][@"closeWorkStruts"] intValue] == 5) {
                        self.closeWorkStrutsLabel.text = @"签退";
                    }else if ([responseObject[@"data"][@"closeWorkStruts"] intValue] == 7) {
                        self.closeWorkStrutsLabel.text = @"缺卡";
                    }
                    self.lineView.hidden = YES;
                    self.LowerTitle.hidden = YES;
                    self.LowerTimeTitle.hidden = YES;
                    self.XiaLowerTitle.backgroundColor = [UIColor grayColor];
                    
                }if ([responseObject[@"data"][@"status"] intValue] == 6){
                    [self.TimeButton setTitle:@"未到打卡时间" forState:UIControlStateNormal];
                    self.TimeButton.enabled = NO;
                    [self.TimeButton setBackgroundImage:[UIImage imageNamed:@"圆"] forState:UIControlStateNormal];
                    self.upWorkStrutsLabel.hidden = YES;
                    self.closeWorkStrutsLabel.hidden = YES;
                    self.XialineView.hidden = YES;
                    self.XiaLowerTitle.hidden = YES;
                    self.XiaLowerTimeTitle.hidden = YES;
                    self.lineView.hidden = NO;
                    self.LowerTitle.hidden = NO;
                    self.LowerTimeTitle.hidden = NO;
                    self.TimeButton.hidden = NO;
                    //                    self.TimeButton.frame = CGRectMake(ScreenWidth / 2 - 200 * px, CGRectGetMaxY(self.XiaLowerTitle.frame) + 50 * px, 400 * px, 400 * px);
                    self.locationButton.frame = CGRectMake(ScreenWidth / 3, CGRectGetMaxY(self.TimeButton.frame)+ 30 * px, ScreenWidth / 3, 60 * px);
                }
            }
            
            
        }else{
            [MBManager showBriefAlert:responseObject[@"msg"]];
            [self.tableView.mj_header endRefreshing];
        }
        
    
    } failure:^(NSError *error) {
        [self.tableView.mj_header endRefreshing];
        [MBManager showBriefAlert:Failed];
    }];
    
}


- (void)viewDidLoad {
    [super viewDidLoad];
    
    //获取经纬度
    [self getLocation];
    
    [self CreatHeaderView];
    
    [self CreattableView];
    //设置定时器
    _countDownTimer = [NSTimer scheduledTimerWithTimeInterval:1 target:self selector:@selector(countDownAction) userInfo:nil repeats:YES];
    //启动倒计时后会每秒钟调用一次方法 countDownAction
    
    
}
-(void)getLocation
{
    //判断定位功能是否打开
    if ([CLLocationManager locationServicesEnabled]) {
        locationmanager = [[CLLocationManager alloc]init];
        locationmanager.delegate = self;
        
        [locationmanager requestAlwaysAuthorization];
        currentCity = [NSString new];
        [locationmanager requestWhenInUseAuthorization];
        
        //设置寻址精度
        locationmanager.desiredAccuracy = kCLLocationAccuracyBestForNavigation;
        locationmanager.distanceFilter = 3.0f;
        [locationmanager startUpdatingLocation];
    }
}
#pragma mark CoreLocation delegate (定位失败)
//定位失败后调用此代理方法
-(void)locationManager:(CLLocationManager *)manager didFailWithError:(NSError *)error
{
//    //设置提示提醒用户打开定位服务
//    UIAlertController *alert = [UIAlertController alertControllerWithTitle:@"允许定位提示" message:@"请在设置中打开定位" preferredStyle:UIAlertControllerStyleAlert];
//    UIAlertAction *okAction = [UIAlertAction actionWithTitle:@"打开定位" style:UIAlertActionStyleDefault handler:nil];
//
//    UIAlertAction *cancelAction = [UIAlertAction actionWithTitle:@"取消" style:UIAlertActionStyleCancel handler:nil];
//    [alert addAction:okAction];
//    [alert addAction:cancelAction];
//    [self presentViewController:alert animated:YES completion:nil];
}

#pragma mark 定位成功后则执行此代理方法
-(void)locationManager:(CLLocationManager *)manager didUpdateLocations:(NSArray<CLLocation *> *)locations
{
    [locationmanager stopUpdatingHeading];
    //旧址
    CLLocation *currentLocation = [locations lastObject];
    CLGeocoder *geoCoder = [[CLGeocoder alloc]init];
    
    
    CLLocationCoordinate2D wgsPt = manager.location.coordinate;
    CLLocationCoordinate2D bdPt = [JZLocationConverter wgs84ToBd09:wgsPt];
    self.jingdu = bdPt.latitude;
    self.weidu = bdPt.longitude;
//    WYCLog(@"self.jingdu--------%f------self.weidu----%f",self.jingdu,self.weidu);
    //反地理编码
    [geoCoder reverseGeocodeLocation:currentLocation completionHandler:^(NSArray<CLPlacemark *> * _Nullable placemarks, NSError * _Nullable error) {
        if (placemarks.count > 0) {
            CLPlacemark *placeMark = placemarks[0];
            currentCity = placeMark.locality;
            if (!currentCity) {
                currentCity = @"无法定位当前城市";
            }
            
            WYCLog(@"--------%@",self.locationName);
            self.locationName = placeMark.name;
            
        }
    }];
    
}



- (void)CreatHeaderView{
    
    self.view.backgroundColor = backgroundcolor;
    
    
    
    
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
    return 1;
}
- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath{
    return 1800 * px;
}
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
    
    
    static NSString *TableSampleIdentifier = @"cellStr";
    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:TableSampleIdentifier];
    tableView.backgroundColor = [UIColor clearColor];
    if (cell == nil) {
        cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleValue1 reuseIdentifier:TableSampleIdentifier];
    }
    if (indexPath.row == 0) {
        cell.backgroundColor = backgroundcolor;
        UIView * HeaderView = [[UIView alloc] initWithFrame:CGRectMake(0, 0, ScreenWidth, 600 * px)];
        HeaderView.backgroundColor = self.view.backgroundColor;
        [cell addSubview:HeaderView];
        
        
        UIImageView * HeaderIMG = [[UIImageView alloc] initWithFrame:CGRectMake(HeaderView.x, -20, HeaderView.width, HeaderView.height - 20 * px)];
        HeaderIMG.image = [UIImage imageNamed:@"hdzx_bg"];
        HeaderIMG.contentMode = UIViewContentModeScaleAspectFill;
        [HeaderView addSubview:HeaderIMG];
        
        
        /** title*/
        UILabel * Navtitle = [[UILabel alloc] init];
        if ([WRNavigationBar isIphoneX]) {
            Navtitle.frame = CGRectMake(ScreenWidth / 2 - 150 * px, 45, 300 * px, 60 * px);
        }else{
            Navtitle.frame = CGRectMake(ScreenWidth / 2 - 150 * px, 20, 300 * px, 60 * px);
            
        }
        Navtitle.textColor = [UIColor whiteColor];
        Navtitle.text = @"考勤打卡";
        Navtitle.textAlignment = NSTextAlignmentCenter;
        Navtitle.font = [UIFont boldSystemFontOfSize:SYRealValue(38 / 2)];
        [HeaderView addSubview:Navtitle];
        
        
        /** 日历按钮*/
        UIImageView * PhotoIMG = [[UIImageView alloc] initWithFrame:CGRectMake(30 * px, CGRectGetMaxY(Navtitle.frame) + 80 * px, 150 * px, 150 * px)];
        [PhotoIMG sd_setImageWithURL:[NSURL URLWithString:[NSString stringWithFormat:@"%@",[WYCAccountTool unarchiveavatarUrl]]] placeholderImage:[UIImage imageNamed:@"我的头像占位符"]];
        PhotoIMG.layer.cornerRadius = 75 * px;
        PhotoIMG.layer.masksToBounds = YES;
        PhotoIMG.backgroundColor = [UIColor grayColor];
        [HeaderView addSubview:PhotoIMG];
        
        
        /** 名字*/
        UILabel * NameLabel = [[UILabel alloc] initWithFrame:CGRectMake(CGRectGetMaxX(PhotoIMG.frame) + 20 * px, PhotoIMG.y + 45 * px, 500 * px, 60 * px)];
        NameLabel.textColor = [UIColor whiteColor];
        NameLabel.text = [NSString stringWithFormat:@"%@",[WYCAccountTool unarchiveuserName]];
        NameLabel.textAlignment = NSTextAlignmentLeft;
        NameLabel.font = [UIFont boldSystemFontOfSize:SYRealValue(30 / 2)];
        [HeaderView addSubview:NameLabel];
        
        
        /** 考勤记录*/
        UIButton * AttendanceButton = [UIButton buttonWithType:UIButtonTypeCustom];
        AttendanceButton.frame = CGRectMake(HeaderView.width - 350 * px, PhotoIMG.y, 350 * px, 100 * px);
        CAGradientLayer * gradientLayer =  [CAGradientLayer layer];
        gradientLayer.frame = AttendanceButton.bounds;
        gradientLayer.startPoint = CGPointMake(0, 0);
        gradientLayer.endPoint = CGPointMake(1, 0);
        gradientLayer.locations = @[@(0.2),@(1.0)];//渐变点
        [gradientLayer setColors:@[(id)[WYCRGBColor(96, 59, 152) CGColor],(id)[WYCRGBColor(202, 72, 114) CGColor]]];//渐变数组
        [AttendanceButton.layer addSublayer:gradientLayer];
        [AttendanceButton setTitle:@"考勤记录" forState:UIControlStateNormal];
        AttendanceButton.titleLabel.font = [UIFont systemFontOfSize:SYRealValue(28 / 2)];
        [AttendanceButton setTitleColor:[UIColor whiteColor] forState:UIControlStateNormal];
        //    [AttendanceButton setImage:[UIImage imageNamed:@"记录"] forState:UIControlStateNormal];
        [AttendanceButton addTarget:self action:@selector(AttendanceButtonClick) forControlEvents:UIControlEventTouchUpInside];
        [HeaderView  addSubview:AttendanceButton];
        UIBezierPath *maskPath = [UIBezierPath bezierPathWithRoundedRect:AttendanceButton.bounds byRoundingCorners:UIRectCornerTopLeft | UIRectCornerBottomLeft cornerRadii:CGSizeMake(50 * px, 50 * px)];
        CAShapeLayer *maskLayer = [[CAShapeLayer alloc] init];
        maskLayer.frame = AttendanceButton.bounds;
        maskLayer.path = maskPath.CGPath;
        AttendanceButton.layer.mask = maskLayer;
        
        
        /** 日历按钮*/
        UIImageView * AttendanceIMG = [[UIImageView alloc] initWithFrame:CGRectMake(30 * px, 20 * px, 40 * px, 55 * px)];
        AttendanceIMG.image = [UIImage imageNamed:@"记录"];
        [AttendanceButton addSubview:AttendanceIMG];
        
        
        /** 日历按钮*/
        UIImageView * AttendanceBackIMG = [[UIImageView alloc] initWithFrame:CGRectMake(280 * px, 30 * px, 40 * px, 40 * px)];
        AttendanceBackIMG.image = [UIImage imageNamed:@"txarrow"];
        [AttendanceButton addSubview:AttendanceBackIMG];
        
        
        
        //设置倒计时显示的时间
        NSString *str_hour = [NSString stringWithFormat:@"%02ld",secondsCountDown/3600];//时
        NSString *str_minute = [NSString stringWithFormat:@"%02ld",(secondsCountDown%3600)/60];//分
        NSString *str_second = [NSString stringWithFormat:@"%02ld",secondsCountDown%60];//秒
        NSString *format_time = [NSString stringWithFormat:@"%@:%@:%@",str_hour,str_minute,str_second];
        //    self.timeLbl.text = [NSString stringWithFormat:@"即将开始：%@",format_time];
        //    //设置文字颜色
        //    self.timeLbl.textColor = [UIColor  blackColor ];
        //    [self.view addSubview:_timeLbl];
        //    NSDate *date=[NSDate date];
        //    NSDateFormatter *format1=[[NSDateFormatter alloc] init];
        //    [format1 setDateFormat:@"YYYY-MM-dd"];
        //    NSString *dateStr;
        //    dateStr = [format1 stringFromDate:date];
        /** */
        UIButton * DateButton = [[UIButton alloc] initWithFrame:CGRectMake(PhotoIMG.x + 30 * px, CGRectGetMaxY(HeaderView.frame) + 30 * px, 300 * px, 80 * px)];
        //    [DateButton setTitle:[NSString stringWithFormat:@"%@",dateStr] forState:UIControlStateNormal];
        DateButton.titleLabel.font = [UIFont systemFontOfSize:SYRealValue(28 / 2)];
        [DateButton  setTitleColor:WYCRGBColor(90, 200, 178) forState:UIControlStateNormal];
        //
        //    [DateButton  addTarget:self action:@selector(DateButtonClick) forControlEvents:UIControlEventTouchUpInside];
        //关键语句
        DateButton.layer.cornerRadius = 10 * px;//2.0是圆角的弧度，根据需求自己更改
        DateButton.layer.borderColor = WYCRGBColor(90, 200, 178).CGColor;//设置边框颜色
        DateButton.layer.borderWidth = 1.0f;//设置边框颜色
        [cell addSubview:DateButton];
        
        self.DateButton = DateButton;
        //    WYCLog(@"------%@",self.status);
        
        
        /** 上班标题*/
        self.JobTitle = [[UILabel alloc] initWithFrame:CGRectMake(DateButton.x, CGRectGetMaxY(DateButton.frame) + 30 * px, 60 * px, 60 * px)];
        self.JobTitle.textColor = [UIColor whiteColor];
        //    self.JobTitle.backgroundColor = FontColor;
        self.JobTitle.layer.cornerRadius = 30 * px;
        self.JobTitle.layer.masksToBounds = YES;
        self.JobTitle.text = @"上";
        self.JobTitle.textAlignment = NSTextAlignmentCenter;
        self.JobTitle.font = [UIFont systemFontOfSize:SYRealValue(26 / 2)];
        [cell addSubview:self.JobTitle];
        
        
        /** 上班标题*/
        self.JobTimeTitle = [[UILabel alloc] initWithFrame:CGRectMake(CGRectGetMaxX(self.JobTitle.frame) + 30 * px, self.JobTitle.y, ScreenWidth / 1.5, 60 * px)];
        self.JobTimeTitle.textColor = [UIColor grayColor];
        //    JobTimeTitle.text = @"上班时间";
        self.JobTimeTitle.textAlignment = NSTextAlignmentLeft;
        self.JobTimeTitle.font = [UIFont systemFontOfSize:SYRealValue(28 / 2)];
        [cell addSubview:self.JobTimeTitle];
        
        self.IMGleftButton = [[MSlocationIMGleftButton alloc] initWithFrame:CGRectMake(self.JobTimeTitle.x, CGRectGetMaxY(self.JobTimeTitle.frame) + 20 * px, ScreenWidth / 1.5, 60 * px)];
        [self.IMGleftButton setTitleColor:[UIColor grayColor] forState:UIControlStateNormal];
        [self.IMGleftButton setTitle:@"北京朝阳区将台镇高尔夫学校公园" forState:UIControlStateNormal];
        [self.IMGleftButton setImage:[UIImage imageNamed:@"定位"] forState:UIControlStateNormal];
        self.IMGleftButton.contentHorizontalAlignment  = UIControlContentHorizontalAlignmentLeft;
        self.IMGleftButton.titleLabel.font = [UIFont systemFontOfSize:SYRealValue(28 / 2)];
        [cell addSubview:self.IMGleftButton];
        
        
        /** 状态*/
        self.upWorkStrutsLabel = [[UILabel alloc] initWithFrame:CGRectMake(self.JobTimeTitle.x, CGRectGetMaxY(self.IMGleftButton.frame)+ 10 * px, 100 * px, 50 * px)];
        self.upWorkStrutsLabel.textAlignment = NSTextAlignmentCenter;
        self.upWorkStrutsLabel.layer.cornerRadius = 10 * px;
        self.upWorkStrutsLabel.backgroundColor = WYCRGBColor(95, 207, 185);
        self.upWorkStrutsLabel.textColor = [UIColor whiteColor];
        self.upWorkStrutsLabel.font = [UIFont systemFontOfSize:SYRealValue(26 / 2)];
        [cell addSubview:self.upWorkStrutsLabel];
        
        
        /** 打卡按钮*/
        self.TimeButton = [[UIButton alloc] initWithFrame:CGRectMake(ScreenWidth / 2 - 200 * px, CGRectGetMaxY(self.IMGleftButton.frame) + 50 * px, 400 * px, 400 * px)];
        [self.TimeButton  setBackgroundImage:[UIImage imageNamed:@"daka_bg"] forState:UIControlStateNormal];
        self.TimeButton.titleLabel.font = [UIFont systemFontOfSize:SYRealValue(35 / 2)];
        [self.TimeButton  setTitleColor:[UIColor whiteColor] forState:UIControlStateNormal];
        [self.TimeButton  addTarget:self action:@selector(ClockButtonClick) forControlEvents:UIControlEventTouchUpInside];
        //    ClockButton.enabled = YES;
        [cell addSubview:self.TimeButton];
        
        
        
        self.ClockButton = [[UIButton alloc] initWithFrame:CGRectMake(0, self.TimeButton.height - 160 * px, self.TimeButton.width, 60 * px)];
        self.ClockButton.titleLabel.font = [UIFont systemFontOfSize:SYRealValue(28 / 2)];
        [self.ClockButton  setTitleColor:[UIColor whiteColor] forState:UIControlStateNormal];
        self.ClockButton.contentHorizontalAlignment = UIControlContentHorizontalAlignmentCenter;
        [self.ClockButton setTitle:[NSString stringWithFormat:@"%@",format_time] forState:UIControlStateNormal];
        //    [timeButton  addTarget:self action:@selector(ClockButtonClick) forControlEvents:UIControlEventTouchUpInside];
        [self.TimeButton addSubview:self.ClockButton];
        //    self.ClockButton = timeButton;
        
        
        /** 范围按钮*/
        self.locationButton = [[MSlocationButton alloc] init];
        self.locationButton.titleLabel.font = [UIFont systemFontOfSize:SYRealValue(28 / 2)];
        [self.locationButton  setTitleColor:[UIColor whiteColor] forState:UIControlStateNormal];
        [cell  addSubview:self.locationButton];
        
        //    //获取当前时间日期
        //
        
        self.lineView = [[UIView alloc] init];
        self.lineView.frame = CGRectMake(self.JobTitle.x + 30 * px, CGRectGetMaxY(self.JobTitle.frame), 1, 700 * px);
        self.lineView.backgroundColor = BackViewColor;
        [cell addSubview:self.lineView];
        
        
        self.XialineView = [[UIView alloc] init];
        self.XialineView.frame = CGRectMake(self.JobTitle.x + 30 * px, CGRectGetMaxY(self.JobTitle.frame), 1, 300 * px);
        self.XialineView.backgroundColor = BackViewColor;
        [cell addSubview:self.XialineView];
        
        
        
        /** 下班标题*/
        self.XiaLowerTitle = [[UILabel alloc] initWithFrame:CGRectMake(self.JobTitle.x, CGRectGetMaxY(self.XialineView.frame), self.JobTitle.width, self.JobTitle.height)];
        self.XiaLowerTitle.textColor = [UIColor whiteColor];
        //    self.LowerTitle.backgroundColor = [UIColor grayColor];
        self.XiaLowerTitle.layer.cornerRadius = 30 * px;
        self.XiaLowerTitle.layer.masksToBounds = YES;
        self.XiaLowerTitle.text = @"下";
        self.XiaLowerTitle.textAlignment = NSTextAlignmentCenter;
        self.XiaLowerTitle.font = [UIFont systemFontOfSize:SYRealValue(26 / 2)];
        [cell addSubview:self.XiaLowerTitle];
        
        
       
        
        
        /** 下班标题*/
        self.LowerTitle = [[UILabel alloc] initWithFrame:CGRectMake(self.JobTitle.x, CGRectGetMaxY(self.lineView.frame), self.JobTitle.width, self.JobTitle.height)];
        self.LowerTitle.textColor = [UIColor whiteColor];
        //    self.LowerTitle.backgroundColor = [UIColor grayColor];
        self.LowerTitle.layer.cornerRadius = 30 * px;
        self.LowerTitle.layer.masksToBounds = YES;
        self.LowerTitle.text = @"下";
        self.LowerTitle.textAlignment = NSTextAlignmentCenter;
        self.LowerTitle.font = [UIFont systemFontOfSize:SYRealValue(26 / 2)];
        [cell addSubview:self.LowerTitle];
        
        
        
        
        /** 下班标题*/
        self.LowerTimeTitle = [[UILabel alloc] initWithFrame:CGRectMake(self.JobTimeTitle.x, self.LowerTitle.y, self.JobTimeTitle.width, self.JobTimeTitle.height)];
        self.LowerTimeTitle.textColor = [UIColor grayColor];
        //    LowerTimeTitle.text = @"下班时间";
        self.LowerTimeTitle.textAlignment = NSTextAlignmentLeft;
        self.LowerTimeTitle.font = [UIFont systemFontOfSize:SYRealValue(28 / 2)];
        [cell addSubview:self.LowerTimeTitle];
        
        
        /** 下班标题*/
        self.XiaLowerTimeTitle = [[UILabel alloc] initWithFrame:CGRectMake(CGRectGetMaxX(self.XiaLowerTitle.frame) + 30 * px, self.XiaLowerTitle.y, self.JobTimeTitle.width, self.JobTimeTitle.height)];
        self.XiaLowerTimeTitle.textColor = [UIColor grayColor];
        //    LowerTimeTitle.text = @"下班时间";
        self.XiaLowerTimeTitle.textAlignment = NSTextAlignmentLeft;
        self.XiaLowerTimeTitle.font = [UIFont systemFontOfSize:SYRealValue(28 / 2)];
        [cell addSubview:self.XiaLowerTimeTitle];
        
        
        /** 状态*/
        self.closeWorkStrutsLabel = [[UILabel alloc] initWithFrame:CGRectMake(self.XiaLowerTimeTitle.x, CGRectGetMaxY(self.XiaLowerTimeTitle.frame) + 10 * px, 100 * px, 50 * px)];
        self.closeWorkStrutsLabel.backgroundColor = WYCRGBColor(95, 207, 185);
        self.closeWorkStrutsLabel.textColor = [UIColor whiteColor];
        self.closeWorkStrutsLabel.textAlignment = NSTextAlignmentCenter;
        self.closeWorkStrutsLabel.layer.cornerRadius = 10 * px;
        self.closeWorkStrutsLabel.font = [UIFont systemFontOfSize:SYRealValue(26 / 2)];
        [cell addSubview:self.closeWorkStrutsLabel];
    }
    return cell;
}
- (void)ClockButtonClick{
    
    WYCLogFunc;
    
    WYCLog(@"111111111111");
    if ([self.TimeButton.titleLabel.text isEqualToString:@"未到打卡时间"]) {
        return;
    }
    if ([self.TimeButton.titleLabel.text isEqualToString:@"未到打卡时间"]) {
        return;
    }
    if ([self.locationButton.titleLabel.text isEqualToString:@"未进入考勤范围"]) {
        [MBManager showBriefAlert:@"未进入考勤范围"];
        return;
    }
    NSString *attendType = @"";
    if ([self.TimeButton.titleLabel.text isEqualToString:@"上班打卡"] || [self.TimeButton.titleLabel.text isEqualToString:@"迟到打卡"]) {
        attendType = @"1";
    }else{
        attendType = @"2";
        
    }
    NSDictionary * params = @{@"uid":[WYCAccountTool unarchiveuid],@"stn_id":[WYCAccountTool unarchivebelongDepid],@"attendType":attendType,@"ste_id":[WYCAccountTool unarchivebelongBusid],@"attendPosition":self.locationName};
    WYCLog(@"%@",params);
    [HTTPRequest POST:MSaddAttendance params:params succeed:^(id responseObject) {
        WYCLog(@"%@",responseObject);
        WYCLog(@"%@",responseObject[@"msg"]);
        if (CodeStr) {
//            dispatch_async(dispatch_get_main_queue(), ^{
            
                [self ReloadData];
                
//            });
        }else{
            [MBManager showBriefAlert:responseObject[@"msg"]];
        }
    } failure:^(NSError *error) {
        
    }];
    
}
//实现倒计时动作
-(void)countDownAction{
    //倒计时-1
    secondsCountDown++;
    
    
    //获取当前时间日期
        NSDate *date = [NSDate date];
        NSDateFormatter *format1=[[NSDateFormatter alloc] init];
        [format1 setDateFormat:@"HH:mm:ss"];
        NSString *dateStr;
        dateStr = [format1 stringFromDate:date];
    
    //修改倒计时标签及显示内容
    [self.ClockButton setTitle:[NSString stringWithFormat:@"%@",dateStr] forState:UIControlStateNormal];
    
    
    //当倒计时到0时做需要的操作，比如验证码过期不能提交
    if(secondsCountDown==0){
        
        [_countDownTimer invalidate];
    }
    
}

- (void)AttendanceButtonClick{
    
    WYCLogFunc;
//    [MBManager showBriefAlert:@"暂无功能!"];
    MSAttendanceCardController *AttendanceVC = [[MSAttendanceCardController alloc] init];
    [self.navigationController pushViewController:AttendanceVC animated:NO];
}
@end
